package cn.yogehaoren.dlx;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * @author WangNing
 * @since 1.0
 */
public class DLXConsumerMain {

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("k8s-node2");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        Connection connection = connectionFactory.newConnection();

        Channel channel = connection.createChannel();

        //1. 声明一个普通的队列和交换机
        String exchangeName = "test_dlx_exchange";
        String queueName = "dlx_queue";
        String routingKey = "dlx.save";

        //2. 设置死信队列属性,必须放在queue中
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "dlx.exchange");
        channel.exchangeDeclare(exchangeName, "fanout", false, false, null);
        channel.queueDeclare(queueName, true, false, false, arguments);
        channel.queueBind(queueName, exchangeName, routingKey);

        // 3. 声明死信队列
        channel.exchangeDeclare("dlx.exchange", "topic", true,false, null);
        channel.queueDeclare("dlx.queque", true, false, false, null);
        channel.queueBind("dlx.queque", "dlx.exchange", "#");



        channel.basicConsume(queueName, true, new DLXCustomerConsumer(channel));


    }

}
